//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  MyCircleBoard.m
//  iHeima
//
//  Created by he chao on 6/4/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "MyCircleBoard.h"
#import "AddCircleBoard.h"

#pragma mark -

@interface MyCircleBoard()<UIWebViewDelegate>
{
	//<#@private var#>
    
    NSString* _id;
    NSString* _action;
    
    UITextField* _commentTF;
    
    UIView* _commentView;
    
    float _keyBoardHei;
    BOOL isCouldMove;
    NSString* _commitBId;
    
    BeeUIWebView* myWebView;
    
    
}
@end

@implementation MyCircleBoard

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = [NSString stringWithFormat:@"%@的黑马圈",kUserInfo[@"username"]];
    [self showNaviBar];
    [self showBarButton:BeeUINavigationBar.LEFT image:IMAGESTRING(@"navi_back")];
    [self showBarButton:BeeUINavigationBar.RIGHT image:IMAGESTRING(@"navi_write")];
    self.view.backgroundColor = RGB(246, 246, 246);
    [self loadContent];
    
    //  链接HTML5 在我的黑马圈
    myWebView = [BeeUIWebView spawn];
 myWebView.url = [NSString stringWithFormat:@"%@/heimaship/myfriendship/uid/%@",kBaseUrl,kUserInfo[@"uid"]];
    
    NSLog(@"我的UID： %@",kUserInfo[@"uid"]);
    
    //myWebView.url = [NSString stringWithFormat:@"%@/heimaship/myfriendship/uid/1",kBaseUrl];
    myWebView.delegate =  self;
    myWebView.frame = CGRectMake(0, 0, self.viewWidth, self.viewHeight-(IOS7_OR_LATER?64:44));
    [self.view addSubview:myWebView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    isCouldMove = YES;
    
    
    _commentView = [[UIView alloc]initWithFrame:CGRectMake(0, 568, 320, 35)];
    _commentView.backgroundColor = [UIColor grayColor];
   
    
    
    
    UIImageView* commentBg = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 270, 25)];
    commentBg.image = [UIImage imageNamed:@"chat_input@2x.png"];
    [_commentView addSubview:commentBg];
    
    _commentTF = [[UITextField alloc]initWithFrame:CGRectMake(15, 5, 320, 25)];
    _commentTF.returnKeyType = UIReturnKeyDone;
    _commentTF.placeholder = @"评论内容";
    [_commentView addSubview:_commentTF];
    
    [_commentTF addTarget:self action:@selector(doneAciton:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.view addSubview:_commentView];
    
    
}





- (void)reloadWebView{
     [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/heimaship/myfriendship/uid/%@",kBaseUrl,kUserInfo[@"uid"]]]]];
}




- (void) keyboardWasShown:(NSNotification *) notif{
    
    if (isCouldMove) {
        NSDictionary *userInfo = [notif userInfo];
        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [aValue CGRectValue];
        _keyBoardHei = keyboardRect.size.height;
        
        
        //change UI frame
        NSTimeInterval animationDuration=0.30f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        
        NSLog(@"keyboardHei:%.1f",_keyBoardHei);
        
        CGRect rect=CGRectMake(0.0f,568-216-_commentView.frame.size.height-150,_commentView.frame.size.width,_commentView.frame.size.height);
        //self.view.frame=rect;
        _commentView.frame = rect;
        [UIView commitAnimations];
        
        NSLog(@"键盘弹起");
        
        int x = _commentView.frame.origin.x;
        int f = _commentView.frame.origin.y;
       int wid =  _commentView.frame.size.width;
        int hei  = _commentView.frame.size.height
        ;
        
        NSLog(@"当前View x:%d,高度：%d，当前VIEWwid:%d,hei:%d",x,f,wid,hei);
    }
    
    isCouldMove = NO;
    
    
    
}

- (void)keyboardWillHide:(NSNotification*)sender{
    
    [self keyBoardHidden];
}



- (void)keyBoardHidden{
    
    [_commentTF resignFirstResponder];
    
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    
    CGRect rect=CGRectMake(0.0f,568,_commentView.frame.size.width,_commentView.frame.size.height);
    //self.view.frame=rect;
    _commentView.frame = rect;
    [UIView commitAnimations];
    
    
    isCouldMove = YES;
    NSLog(@"键盘收下");
    int f = _commentView.frame.origin.y;
    NSLog(@"当前View高度：%d",f);
    
}




- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    
    
    NSString* nextUrl = [request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if([nextUrl componentsSeparatedByString:@"#"].count == 5){
        
        [webView stopLoading];
        
        NSArray* ac = [nextUrl componentsSeparatedByString:@"#"];
        NSArray* arr = [[ac objectAtIndex:2] componentsSeparatedByString:@"&"];
        NSLog(@"----%@",[arr objectAtIndex:0]);
        NSLog(@"=====%@",[arr objectAtIndex:1]);
        
        //action
        _action =[[[arr objectAtIndex:0] componentsSeparatedByString:@"="] objectAtIndex:1];
        NSLog(@"动作：%@",[[[arr objectAtIndex:0] componentsSeparatedByString:@"="] objectAtIndex:1]);
        _id = [[[arr objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1];
        NSLog(@"id:%@",[[[arr objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1]);
        
        if ([_action isEqualToString:@"laud"] && _id) {
            //点赞
            [self laudPersonById:_id];
            
        }else if ([_action isEqualToString:@"commit"] && _id){
            //评论
            [self keyBoardShow];
            _commitBId = _id;
            
            
        }
        
    }
    
    
    return YES;
}
//
- (void)laudPersonById:(NSString*)bid{
    
   BeeHTTPRequest* request  = [self POST:[NSString stringWithFormat:@"%@/apifeeds/likeit",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"bid",bid).PARAM(@"type",@"2");
    request.tag = 9998;
}


- (void)commentWithBid:(NSString*)bid{
    BeeHTTPRequest* request = [self POST:[NSString stringWithFormat:@"%@/apifeeds/comment",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"bid",bid).PARAM(@"content",_commentTF.text);
    request.tag = 9999;
    
}

- (void)handleRequest:(BeeRequest *)request

{
    

    NSLog(@"---打印REQUEST tag:%d",request.tag);
    
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        id json = [request.responseString mutableObjectFromJSONString];
       
        if ([json[@"status"] intValue] == 1) {
            
            NSString* title;
            switch (request.tag) {
                case 9998:{
                    
                    title = @"点赞成功" ;
                    
                    
                    break;
                }
                case 9999:
                    
                    NSLog(@"点击评论");
                    title = @"评论成功" ;
                    
                    
                    break;
                
            }
            
            UIAlertView* av = [[UIAlertView alloc]initWithTitle:title message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:Nil, nil];
            [av show];;

        }
        
    }
}

- (void)keyBoardShow{
    
    [_commentTF becomeFirstResponder];
    isCouldMove = NO;
    
    
}
- (void)doneAciton:(UITextField*)sender{
    
    [self keyBoardHidden];
    
    [self commentWithBid:_commitBId];
    
    
}







ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAction) name:@"myNoti" object:nil];
}

- (void)notiAction{
   
    [self reloadWebView];
}

ON_DID_APPEAR( signal )
{
    
    
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.stack popBoardAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
    AddCircleBoard *board = [[AddCircleBoard alloc] init];
    [self.stack pushBoard:board animated:YES];
}

- (void)loadContent{
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

@end
